﻿
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml;

public class ItemResourceManager
{
    private DressListInfo _dressList;
    private Dictionary<int, CombinationSuitInfo> m_combination_suit_info_map;
    private NewFeatureList m_new_features = new NewFeatureList();

    private List<CombinationSuitInfo> m_advanced_combin_suit_list;
    public virtual IEnumerator Init()
    {
        if (Consts.EXAMPLE1_USE_BYTES)
        {
            yield return ReadConfigWithBytes();
        }
        else
        {
            yield return ReadConfigWithXml();
        }
    }

    private IEnumerator ReadConfigWithBytes()
    {
        yield return ReadDressConfigWithBytes(XmlPathDefines.Dress);
    }

    private IEnumerator ReadConfigWithXml()
    {
        string filePath = Path.Combine(Consts.LOGIC_SOURCE_CONFIG_DIR, XmlPathDefines.Dress);
        yield return ReadDressConfigWithXml(filePath);
    }

    private IEnumerator ReadDressConfigWithBytes(string filePath)
    {
        DressListInfo dressList = XMLBufFileReader.ReadXmlBuf(filePath) as DressListInfo;
        if (dressList == null)
        {
            LogWrapper.LogError("[ItemResourceManager][ReadDressConfigWithBytes] read ", filePath, " error");
            yield break;
        }
    }

    private IEnumerator ReadDressConfigWithXml(string filePath)
    {
        XmlDocument xmlDoc = new XmlDocument();
        XmlReaderSettings settings = new XmlReaderSettings();
        settings.IgnoreComments = true;
        string xmlContent = FileUtil.ReadFileToString(filePath, EnumFileType.STREAMING_ASSETS);
        StringReader strUTF8ConReader = new StringReader(xmlContent);
        XmlReader reader = XmlReader.Create(strUTF8ConReader, settings);
        xmlDoc.Load(reader);
        XmlNode rootNode = xmlDoc.LastChild;

        DressListInfo dressInfo = new DressListInfo();
        XmlNodeList dressList = rootNode.ChildNodes;
        for (int idx = 0; idx < dressList.Count; ++idx)
        {
            var dressNode = dressList[idx];
            if (dressNode.Name == "DressList")
            {
                if (!ParserDressList(ref dressInfo, dressNode))
                {
                    LogWrapper.LogError("ReadDressConfigWithXml error on parse DressList");
                }
            }
        }
        yield break;
    }

    public static bool ParserDressList(ref DressListInfo info, XmlNode dressListNode)
    {
        XmlNodeList nodelist = dressListNode.ChildNodes;
        int count = nodelist.Count;
        for (int i = 0; i < count; i++)
        {
            XmlNode dressNode = nodelist[i];
            Dress dt = new Dress();
            if (ParserDress(ref dt, dressNode) == false)
            {
                return false;
            }
            info.dress_list.Add(dt);
        }
        return true;
    }

    private static bool ParserDress(ref Dress info, XmlNode itemNode)
    {
        if (ParserItemBase(ref info, itemNode) == false)
        {
            return false;
        }
        if (ParserBodyPartList(ref info, itemNode) == false)
        {
            return false;
        }
        return true;
    }

    private static bool ParserItemBase(ref Dress info, XmlNode itemNode)
    {
        try
        {
            info.id = int.Parse(itemNode.Attributes["id"].Value);
            info.name = itemNode.Attributes["name"].Value;
            info.gender = int.Parse(itemNode.Attributes["gender"].Value);
            info.type = int.Parse(itemNode.Attributes["type"].Value);
            info.sub_type = int.Parse(itemNode.Attributes["sub_type"].Value);

            XmlAttribute iconAttr = itemNode.Attributes["icon"];
            if (null != iconAttr)
            {
                info.icon = iconAttr.Value;
            }
            else
            {
                string strTempID = info.id.ToString();
                if (0 < Consts.ICON_NAME_LENGTH - strTempID.Length)
                {
                    strTempID = strTempID.PadLeft(Consts.ICON_NAME_LENGTH, '0');
                }
                info.icon = strTempID + "_icon";
            }

            XmlAttribute introAttr = itemNode.Attributes["intro"];
            info.intro = (null != introAttr) ? introAttr.Value : "";

            XmlAttribute starLevel = itemNode.Attributes["star_level"];
            info.star_level = (null != starLevel) ? XmlConvert.ToInt32(starLevel.Value) : info.star_level;

            XmlAttribute canOverlay = itemNode.Attributes["can_overlay"];
            info.can_overlay = (null != canOverlay) ? XmlConvert.ToBoolean(canOverlay.Value) : info.can_overlay;

            XmlAttribute isLimit = itemNode.Attributes["limit"];
            info.limit = (null != isLimit) ? XmlConvert.ToBoolean(isLimit.Value) : info.limit;
            info.item_level = (null != itemNode.Attributes["item_level"]) ? XmlConvert.ToInt32(itemNode.Attributes["item_level"].Value) : info.item_level;
            info.show_inbag = (null != itemNode.Attributes["show_inbag"]) ? XmlConvert.ToBoolean(itemNode.Attributes["show_inbag"].Value) : info.show_inbag;
            info.safe_Show = (null != itemNode.Attributes["safe_show"]) ? XmlConvert.ToBoolean(itemNode.Attributes["safe_show"].Value) : info.safe_Show;
            return true;
        }
        catch
        {
            LogWrapper.LogError("ParserDress error : id:", info.id, "; name:", info.name);
            return false;
        }
    }

    private static bool ParserBodyPartList(ref Dress info, XmlNode itemNode)
    {
        try
        {

            XmlElement bodypartListEle = itemNode["bodyPartList"];
            if (null != bodypartListEle)
            {
                XmlNodeList bodypartNodeList = bodypartListEle.ChildNodes;
                for (int i = 0; i < bodypartNodeList.Count; ++i)
                {
                    XmlNode bodypartNode = bodypartNodeList[i] as XmlNode;
                    if (null != bodypartNode)
                    {
                        bodyPart bodyPartInfo = new bodyPart();
                        bodyPartInfo.type = XmlConvert.ToInt32(bodypartNode.Attributes["type"].Value);
                        bodyPartInfo.src = bodypartNode.Attributes["src"].Value;

                        info.bodypartlist.Add(bodyPartInfo);
                    }
                }

            }
            return true;
        }
        catch
        {
#if USE_LOGWRAPPER
            LogWrapper.LogError("ParserDress error ");
#endif
            return false;
        }
    }
}
